﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using UtilityLibrary;

namespace MapLibrary.Players.Skills
{
    class PowerShield : Skill
    {
        /// <summary>
        /// The total possible life of the shield
        /// </summary>
        private LevelDepValue<int> maxShieldLife;

        /// <summary>
        /// The number of frames for an increase of the shield
        /// life by 1.
        /// </summary>
        private LevelDepValue<int> shieldRechargeRate;

        /// <summary>
        /// The different damage categories that the shield
        /// protects against.
        /// </summary>
        private LevelDepValue<VulnerabilityProfile> damageProtection;

        /// <summary>
        /// Fixed number of frames until a decrease of the
        /// shield life by 1 while it is activated.
        /// </summary>
        private int framesPerDecay;

        /// <summary>
        /// Whether or not the shield is activated.
        /// </summary>
        public bool Active;

        /// <summary>
        /// The number of frames that the shield has
        /// been activated since the last delay tick.
        /// </summary>
        private int decayCount;

        /// <summary>
        /// The amount of life the shield
        /// currently has.
        /// </summary>
        private int shieldLife;

        /// <summary>
        /// The number of frames the shield has
        /// been deactivated since the last regen
        /// tick.
        /// </summary>
        private int regenCount;

        public PowerShield()
            : base()
        {
            SetLevelThresholds();

            Active = false;
            framesPerDecay = 30;
            decayCount = 0;
            regenCount = 0;
            shieldLife = maxShieldLife.Value;
        }

        public override void Update()
        {
            if(Active){
                UpdateDecay();
                if (shieldLife <= 0.0f)
                {
                    Active = false;
                }
            }
            else
                UpdateRegen();
        }

        public int ShieldLife
        {
            get { return this.shieldLife; }
            set { this.shieldLife = value; }
        }

        public override void DrawAtPlayer(SpriteBatch batch, WorldViewport viewport, Player p)
        {
            return;
            Vector2 position = viewport.WorldToViewportCoords(p.Position);
            position = position - p.GetCenter() - new Vector2(0, 14);

            if (Active)
                PrimitiveBrush.RenderText(batch, "Shield Life (Activated): " + shieldLife, position);
            else
                PrimitiveBrush.RenderText(batch, "Shield Life (Deactivated): " + shieldLife, position);
        }

        public override void Activate(Player p)
        {
            Active = !Active;
        }

        public override bool ActivateConditionsSatisfied(Player p)
        {
            return shieldLife > 0;
        }

        private void UpdateDecay()
        {
            regenCount = 0;
            decayCount++;

            if (decayCount >= framesPerDecay)
            {
                decayCount = 0;
                shieldLife--;
                shieldLife = Math.Max(shieldLife, 0);
            }
        }

        private void UpdateRegen()
        {
            decayCount = 0;
            regenCount++;

            if (regenCount >= shieldRechargeRate.Value)
            {
                regenCount = 0;
                shieldLife++;
                shieldLife = Math.Min(shieldLife, maxShieldLife.Value);
            }
        }

        private void SetLevelThresholds()
        {
            maxShieldLife = new LevelDepValue<int>(this);
            maxShieldLife.AddValueForLevel(1, 100);
            maxShieldLife.AddValueForLevel(3, 200);

            shieldRechargeRate = new LevelDepValue<int>(this);
            shieldRechargeRate.AddValueForLevel(1, 60);
            shieldRechargeRate.AddValueForLevel(4, 30);

            damageProtection = new LevelDepValue<VulnerabilityProfile>(this);
            damageProtection.AddValueForLevel(
                1,
                new VulnerabilityProfile(new Player.DamageCategories[]
                {
                    Player.DamageCategories.Mucus,
                    Player.DamageCategories.Physical,
                    Player.DamageCategories.Water
                }));
            damageProtection.AddValueForLevel(
                2,
                new VulnerabilityProfile(new Player.DamageCategories[]
                {
                    Player.DamageCategories.Physical,
                    Player.DamageCategories.Water
                }));
            damageProtection.AddValueForLevel(
                5,
                new VulnerabilityProfile(new Player.DamageCategories[]
                {
                    Player.DamageCategories.Water
                }));
        }

        public override int GetAmount(string trackerName)
        {
            return shieldLife;
        }

        public override int GetMax(string trackerName)
        {
            return maxShieldLife.Value;
        }

        public override bool CheckAndExecuteLevelUp()
        {
            bool hasLevelUp = base.CheckAndExecuteLevelUp();

            if (hasLevelUp)
            {
                shieldLife = maxShieldLife.Value;
            }

            return hasLevelUp;
        }
    }
}
